/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;

/**
 *
 * @author HoangLiem
 */
public class ListenResult extends Thread {
    
    private Socket socket;
    private String query;
    private JTabbedPane jTpTableData;
    private JTextArea textArea;
    private JTable table;
    
    public ListenResult(Socket socket, String query, JTabbedPane jTpTableData, JTextArea textArea, JTable table) {
        this.socket = socket;
        this.query = query;
        this.jTpTableData = jTpTableData;
        this.textArea = textArea;
        this.table = table;
        start();
    }
    
    public void run() {
        try {
            String[] s = query.split(" ");
            String select = "";
            if (query.length() >= 7) {
                select = query.substring(0, 7);
            }
            if (s[0].equalsIgnoreCase("select") || select.equalsIgnoreCase("select*")) {
                InputStreamReader isr = new InputStreamReader(socket.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String message = br.readLine();
                textArea.setText(message);
                ReadSelectInfo readSelectInfo = new ReadSelectInfo(socket, jTpTableData, textArea, table);
                readSelectInfo.start();
            } else {
                this.jTpTableData.setSelectedIndex(0);
                InputStreamReader isr = new InputStreamReader(socket.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String message = br.readLine();
                textArea.setText(message);
            }
        } catch (Exception ex) {
            Logger.getLogger(ListenResult.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
